const express = require('express');

const app = express();

const fs = require("fs");

const puppeteer = require('puppeteer');

const browserUrl = fs.readFileSync("chrome.txt", "utf8");

app.get('*', function (req, res) {

  const url = req.protocol + '://' + req.hostname + req.originalUrl;

  const ua = req.headers['user-agent'];

  (async () => {

    const browser = await puppeteer.connect({ browserWSEndpoint: browserUrl });

    const page = await browser.newPage(); //创建一个页面.

    try {

      await page.goto(url); //到指定页面的网址.

      await page.waitFor(500);

    }

    catch (err) {

      await page.close();

      await browser.disconnect();

      console.log('出现错误：' + err); // 这里捕捉到错误 `error`

    }

    res.send(await page.content());

    await page.close();

    await browser.disconnect();

  })();

});

const server = app.listen(3000, '127.0.0.1', function () {

  const host = server.address().address;

  const port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});
